package com.example.j202411.viewmodel;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;


import com.example.j202411.contract.PetsContact;
import com.example.j202411.Model.PetEntity;
import com.example.j202411.service.PetsService;

import java.util.Objects;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;


public class PetsModel implements PetsContact.PetsModel {

    private PetsContact.PetsView view;
    private Context context;

    public PetsModel(PetsContact.PetsView view, Context context) {
        this.view = view;
        this.context = context;
    }

    @Override
    public void getData(int type ,String name) {
        String key = "e16001c9c9f1f96b7d9e62860fa411f6";
        String url = "http://apis.juhe.cn/fapigx/";


        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .build();
        PetsService service = retrofit.create(PetsService.class);
        Log.d("1234455565", "运行到此处");
        if (name ==null){
            service.getData(type, key)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(new Observer<PetEntity>() {
                        @Override
                        public void onSubscribe(@NonNull Disposable d) {
                            Log.d("loading......", "获取中");
                        }

                        @Override
                        public void onNext(@NonNull PetEntity petEntity) {
                            if (petEntity==null){
                                Log.d("1515", "onNext: ");
                            }else {
                                if (view != null) {
                                    view.showData(petEntity);
                                }else {
                                    Log.d("空指针异常", "view为null: ");
                                }
                            }

                        }

                        @Override
                        public void onError(@NonNull Throwable e) {
                            Log.d("51818184518518518184561845", Objects.requireNonNull(e.getMessage()));
                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }else {
            service.getData2(name, key)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(new Observer<PetEntity>() {
                        @Override
                        public void onSubscribe(@NonNull Disposable d) {
                            Log.d("loading......", "获取中");
                        }

                        @Override
                        public void onNext(@NonNull PetEntity petEntity) {
                            if (petEntity.getError_code()==0){
                                Log.d("1515", "onNext: ");
                                if (view != null) {
                                    view.showData(petEntity);
                                }else {
                                    Log.d("空指针异常", "view为null: ");
                                }
                            }else {
                                Toast.makeText(context, "该品种不存在", Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onError(@NonNull Throwable e) {
                            Log.d("51818184518518518184561845", Objects.requireNonNull(e.getMessage()));
                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }



    }
}
